﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第二次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int[] numList = new int[200];            //数组全局变量
        int len = 0;                                        //用于标记数组长度
        bool enterTag = false;                      //用于提示用户是否输入空格结尾
        /// <summary>
        /// 文本框输入回车后的事件函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void List_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;
            if (ch == '\r')
            {
                enterTag = true;
                string str = List.Text;
                string[] sArray = str.Split(' ');
                foreach (string i in sArray)
                {
                    numList[len] = Convert.ToInt32(i);
                    len++;
                }
            }
        }
        /// <summary>
        /// 求最大值按钮事件函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Max_Click(object sender, EventArgs e)
        {
            if (enterTag)
            {
                int max = numList[0];
                for (int i = 0; i < len; i++)
                {
                    if (numList[i] > max)
                        max = numList[i];
                }
                MessageBox.Show("最大值是：" + max.ToString());
            }
            else
                MessageBox.Show("您忘记在结尾输入回车了！");
        }
        /// <summary>
        /// 求最小值按钮事件函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Min_MouseClick(object sender, MouseEventArgs e)
        {
            if (enterTag)
            {
                int min = numList[0];
                for (int i = 0; i < len; i++)
                {
                    if (numList[i] < min)
                        min = numList[i];
                }
                MessageBox.Show("最小值是：" + min.ToString());
            }
            else
                MessageBox.Show("您忘记在结尾输入回车了！");
        }
        /// <summary>
        /// 求平均值按钮事件函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Ave_Click(object sender, EventArgs e)
        {
            if (enterTag)
            {
                double ave = 0;
                for (int i = 0; i < len; i++)
                {
                    ave += Convert.ToDouble(numList[i]) / len;
                }
                MessageBox.Show("平均值是：" + ave.ToString());
            }
            else
                MessageBox.Show("您忘记在结尾输入回车了！");
        }
        /// <summary>
        /// 求和按钮事件函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sum_Click(object sender, EventArgs e)
        {
            if (enterTag)
            {
                int sum = 0;
                for (int i = 0; i < len; i++)
                {
                    sum += numList[i];
                }
                MessageBox.Show("元素和是：" + sum.ToString());
            }
            else
                MessageBox.Show("您忘记在结尾输入回车了！");
        }
    }
}
